package com.example.framework.mq.rocketmq.config;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(RocketMQProperties.class)
@ConditionalOnProperty(prefix = "mq.rocketmq", value = "enable")
public class RocketMQAutoConfiguration {

    /**
     * 注册mq生产者
     *
     * @return mq生产者
     */
    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public DefaultMQProducer defaultMQProducer(RocketMQProperties rocketMQProperties) {
        // 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer(rocketMQProperties.getProducerGroup());
        // 设置NameServer的地址
        producer.setNamesrvAddr(rocketMQProperties.getNamesrvAddr());
        return producer;
    }

}
